package cn.touna.jss.modules.custom.tobaccouser.service.impl;

import cn.touna.jss.modules.custom.tobaccouser.entity.RetailRecord;
import cn.touna.jss.modules.custom.tobaccouser.entity.RetailRecordExample;
import cn.touna.jss.modules.custom.tobaccouser.entity.TobaccoUser;
import cn.touna.jss.modules.custom.tobaccouser.entity.TobaccoUserExample;
import cn.touna.jss.modules.custom.tobaccouser.mapper.RetailRecordMapper;
import cn.touna.jss.modules.custom.tobaccouser.mapper.TobaccoUserMapper;
import cn.touna.jss.modules.custom.tobaccouser.service.TobaccoService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: xiaoli
 * @date: 2019/3/11 19:35
 * @Desc:
 **/
@Service
public class TobaccoServiceImpl implements TobaccoService {


    @Autowired
    TobaccoUserMapper tobaccoUserMapper;

    @Autowired
    RetailRecordMapper retailRecordMapper;

    @Override
    public JSONObject getTobaccoData(TobaccoUser entity) {
        TobaccoUserExample ex = new TobaccoUserExample();
        TobaccoUserExample.Criteria criteria = ex.createCriteria();
        criteria.andMobileEqualTo(entity.getMobile());
//        if(entity.getName()!=null){
//            criteria.andNameEqualTo(entity.getName());
//        }
//        if(entity.getIdCard()!=null){
//            criteria.andIdCardEqualTo(entity.getIdCard());
//        }

        List<TobaccoUser> list=  tobaccoUserMapper.selectByExample(ex);
        JSONObject result = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        if(!CollectionUtils.isEmpty(list)){
            TobaccoUser tobaccoUser =  list.get(0);
            RetailRecordExample example = new RetailRecordExample();
            RetailRecordExample.Criteria cr = example.createCriteria();
            String retailCode = tobaccoUser.getRetailCode();
            result.put("isRetail","1");
            if(retailCode!=null){
                cr.andRetailCodeEqualTo(tobaccoUser.getRetailCode());
                List<RetailRecord> retailRecordList=  retailRecordMapper.selectByExample(example);
                result = (JSONObject) JSONObject.toJSON(tobaccoUser);
                result.remove("id");
                result.remove("channel");

                for (RetailRecord retailRecord :retailRecordList){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("purchaseTime",retailRecord.getPurchaseTime());
                    jsonObject.put("purchaseAmount",retailRecord.getPurchaseAmount());
                    jsonArray.add(jsonObject);
                }
            }

          }else{
               result.put("name",entity.getName());
               result.put("mobile",entity.getMobile());
               result.put("idCard",entity.getIdCard());
               result.put("isRetail","0");
        }
        result.put("purchaseList",jsonArray);
        return result;
    }




    @Override
    public List<TobaccoUser> queryTobaccoUserList(TobaccoUser entity) {
        return tobaccoUserMapper.queryTobaccoUserList(entity);
    }

    @Override
    public boolean checkUser(TobaccoUser entity) {
        TobaccoUserExample ex = new TobaccoUserExample();
        TobaccoUserExample.Criteria criteria = ex.createCriteria();
        criteria.andMobileEqualTo(entity.getMobile());
        if(entity.getName()!=null){
            criteria.andNameEqualTo(entity.getName());
        }
//        criteria.andNameIsNotNull();
//        if(entity.getIdCard()!=null){
//            criteria.andIdCardEqualTo(entity.getIdCard());
//        }
        criteria.andMobileIsNotNull();
        List<TobaccoUser> list=  tobaccoUserMapper.selectByExample(ex);
        if(!CollectionUtils.isEmpty(list)){
            return true;
        }
        return false;
    }
}
